<template>
  <Layout>
    <div class="profile-container">
      <!-- 用户基本信息卡片 -->
      <a-card class="profile-card">
        <div class="profile-header">
          <Upload :customRequest="handleFileChange" :showUploadList="false">
            <div class="avatar-upload">
              <a-avatar :size="120" :src="userStore.userInfo?.avatar || 'https://api.dicebear.com/7.x/avatars/svg?seed=1'" class="profile-avatar" />
              <div class="avatar-overlay">
                <CameraOutlined />
                <span>更换头像</span>
              </div>
            </div>
          </Upload>
          <div class="profile-info">
            <h2>{{ userStore.userInfo?.nickname || '未设置昵称' }}</h2>
            <a-tag class="uid-tag" color="green" >UID: {{ userStore.userInfo?.id || 'photographer' }}</a-tag>
            <p class="bio">{{ userStore.userInfo?.description || '这个人很懒，什么都没写~' }}</p>
            <div class="equipment-tags">
              <a-tag>尼康Z9</a-tag>
              <a-tag>24-70mm f/2.8</a-tag>
              <a-tag>70-200mm f/2.8</a-tag>
            </div>
          </div>
        </div>
        <div class="profile-stats">
          <div class="stat-item">
            <h3>{{ userStore.userInfo?.artboardCount || 0 }}</h3>
            <span>作品</span>
          </div>
          <div class="stat-item">
            <h3>{{ userStore.userInfo?.followerCount || 0 }}</h3>
            <span>关注者</span>
          </div>
          <div class="stat-item">
            <h3>{{ userStore.userInfo?.attentionCount || 0 }}</h3>
            <span>关注中</span>
          </div>
        </div>
        <div class="profile-actions">
          <a-button type="primary" block @click="() => router.push('/edit-profile')">编辑资料</a-button>
          <a-button block class="portfolio-button">管理作品集</a-button>
        </div>
      </a-card>

      <!-- 作品展示区 -->
      <a-tabs class="profile-tabs">
        <a-tab-pane key="portfolio" tab="画板">
          <div class="portfolio-grid">
            <div v-for="board in userBoards" :key="board.id" class="portfolio-item">
              <a-card hoverable class="photo-card">
                <template #cover>
                  <div :class="{'image-grid': board.images.length > 1, 'single-image-container': board.images.length === 1}">
                    <template v-if="board.images.length > 1">
                      <div v-for="(image, index) in board.images.slice(0, 4)" :key="image.id" class="grid-image" :class="[`grid-image-${board.images.length}`]">
                        <ProgressiveImage
                          :original-src="image.img_url || 'https://picsum.photos/400/300'"
                          :thumbnail-src="image.img_url ? `${image.img_url}?w=50` : 'https://picsum.photos/50/50'"
                          :alt="board.title"
                        />
                      </div>
                    </template>
                    <template v-else-if="board.images.length === 1">
                      <ProgressiveImage
                        :original-src="board.images[0]?.img_url || 'https://picsum.photos/400/300'"
                        :thumbnail-src="board.images[0]?.img_url ? `${board.images[0].img_url}?w=50` : 'https://picsum.photos/50/50'"
                        :alt="board.title"
                      />
                    </template>
                    <template v-else>
                      <div class="empty-board">
                        <picture-outlined class="empty-icon" />
                        <p class="empty-text">开始创作，上传你的第一张作品</p>
                        <a-button type="primary" size="large" class="upload-btn">上传图片</a-button>
                      </div>
                    </template>
                  </div>
                </template>
                <a-card-meta :title="board.title">
                  <template #description>
                    <div class="photo-meta">
                      <div class="tags-container">
                        <a-tag v-for="tag in board.tags.split(',')" :key="tag" color="blue">{{ tag.replace('#', '') }}</a-tag>
                      </div>
                      <p class="board-description">{{ board.description }}</p>
                      <div class="stats-container">
                        <span><eye-outlined /> {{ board.images[0]?.views_count || 0 }}</span>
                        <span><download-outlined /> {{ board.images[0]?.download_count || 0 }}</span>
                        <span><fire-outlined /> {{ board.hot_ness }}</span>
                      </div>
                    </div>
                  </template>
                </a-card-meta>
              </a-card>
            </div>
          </div>
        </a-tab-pane>
        <a-tab-pane key="collections" tab="收藏">
          <a-empty description="暂无收藏的作品" />
        </a-tab-pane>
        <a-tab-pane key="stories" tab="图文">
          <a-empty description="暂无图文故事" />
        </a-tab-pane>
      </a-tabs>
    </div>
  </Layout>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { Upload } from 'ant-design-vue';
import { CameraOutlined, EyeOutlined, DownloadOutlined, FireOutlined } from '@ant-design/icons-vue';
import Layout from '../components/Layout.vue';
import { useUserStore } from '../store/user';
import { getUserBoards, type UserBoard } from '../api/user';
import ProgressiveImage from '../components/ProgressiveImage.vue';

const router = useRouter();
const userStore = useUserStore();
const uploading = ref(false);
const userBoards = ref<UserBoard[]>([]);

const handleFileChange = async ({ file }: { file: File }) => {
  if (!file) return;

  // 验证文件类型
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('只能上传 JPG/PNG 格式的图片！');
    return;
  }

  // 验证文件大小（限制为2MB）
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('图片大小不能超过2MB！');
    return;
  }

  try {
    uploading.value = true;
    
    // 调用上传头像API
    const response = await uploadAvatar(file);
    
    if (response.data.code === 0) {
      // 更新用户头像信息
      userStore.fetchUserInfo();
      message.success('头像上传成功');
    } else {
      throw new Error('上传失败');
    }
  } catch (error) {
    message.error('头像上传失败');
    console.error('上传头像失败:', error);
  } finally {
    uploading.value = false;
  }
};

onMounted(async () => {
  // 获取用户基本信息
  await userStore.fetchUserInfo();
  
  try {
    const response = await getUserBoards();
    userBoards.value = response.data;
  } catch (error) {
    console.error('获取用户作品集失败:', error);
  }
});
</script>

<style scoped>

.avatar-upload {
  position: relative;
  display: inline-block;
  cursor: pointer;
}

.profile-avatar {
  transition: filter 0.3s;
}

.avatar-upload:hover .profile-avatar {
  filter: brightness(0.8);
}
.avatar-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.3s;
  color: white;
}

.avatar-upload:hover .avatar-overlay {
  opacity: 1;
}

.avatar-overlay span {
  margin-top: 4px;
  font-size: 12px;
}

.profile-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.profile-card {
  margin-bottom: 24px;
  background: #fff;
  border-radius: 8px;
}

.profile-header {
  display: flex;
  align-items: flex-start;
  margin-bottom: 24px;
  padding-left: 40px;
}

.profile-avatar {
  border: 4px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.profile-info {
  padding-right: 100px;
  margin-left: 24px;
  flex: 1;
  position: relative;
}

.uid-tag {
  position: absolute;
  top: 0;
  right: 0;
}

.profile-info h2 {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
}

.username {
  margin: 4px 0;
  color: rgba(0, 0, 0, 0.45);
  font-size: 16px;
}

.bio {
  margin: 8px 0;
  color: rgba(0, 0, 0, 0.85);
  font-size: 16px;
  line-height: 1.5;
}

.equipment-tags {
  margin-top: 12px;
}

.equipment-tags :deep(.ant-tag) {
  margin: 4px 8px 4px 0;
  padding: 4px 8px;
  font-size: 14px;
}

.profile-stats {
  display: flex;
  justify-content: space-around;
  margin-bottom: 24px;
  text-align: center;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
  padding: 16px 0;
}

.stat-item h3 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #262626;
}

.stat-item span {
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;
}

.profile-actions {
  display: flex;
  gap: 12px;
}

.profile-actions :deep(.ant-btn) {
  &:hover, &:focus {
    color: #fff;
    border-color: #6893AC;
    background: #6893AC;
  }
}

.profile-actions :deep(.ant-btn-primary) {
  background: #95C7E0;
  border-color: #95C7E0;
  color: #fff;

  &:hover, &:focus {
    background: #6893AC;
    border-color: #6893AC;
  }
}

.portfolio-button {
  color: #95C7E0;
  border-color: #95C7E0;

  &:hover, &:focus {
    color: #fff;
    border-color: #6893AC;
    background: #6893AC;
  }
}

.profile-tabs {
  background: #fff;
  padding: 16px;
  border-radius: 8px;
}

.portfolio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  padding: 20px 0;
}

.photo-card {
  aspect-ratio: 1;
  transition: transform 0.3s;

  &:hover {
    transform: translateY(-5px);
  }
}

.portfolio-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 4px;
  aspect-ratio: 1;
}

.grid-image {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.grid-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 单张图片样式 */
.single-image-container {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 8px;
  background-color: #f5f5f5;
}

.single-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-color: #f5f5f5;
}

.photo-meta {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
}

.board-description {
  color: #666;
  font-size: 14px;
  margin: 8px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.stats-container {
  display: flex;
  gap: 16px;
  color: #999;
  font-size: 14px;

  span {
    display: flex;
    align-items: center;
    gap: 4px;
  }
}
.image-grid {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  gap: 2px;
  overflow: hidden;
  border-radius: 8px;
}

.grid-image {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.grid-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 单张图片样式 */
.single-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* 两张图片样式 */
.image-grid:has(.grid-image-2) {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: 1fr;
}

/* 三张图片样式 */
.image-grid:has(.grid-image-3) {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
}

.grid-image-3:first-child {
  grid-column: span 2;
}

/* 四张图片样式 */
.image-grid:has(.grid-image-4) {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
}
.empty-board {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
  border-radius: 8px;
  padding: 24px;
}

.empty-icon {
  font-size: 48px;
  color: #95C7E0;
  margin-bottom: 16px;
}

.empty-text {
  color: #666;
  font-size: 16px;
  margin-bottom: 24px;
  text-align: center;
}

.upload-btn {
  background: #95C7E0;
  border-color: #95C7E0;
  
  &:hover, &:focus {
    background: #6893AC;
    border-color: #6893AC;
  }
}
</style>